<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.js'></script>
</head>

<body>
    <div id='app'>
        <button v-for="item in btnList" @click="btnClick(item)">{{item}}</button>
        <h1>{{beforeValue}}{{operator}}{{afterValue}}{{resultqqq}}</h1>
    </div>
</body>
<script>
    const app = {
        data() {
            return {
                btnList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '+', '-', '*', '/', 'C', '='],
                beforeValue: "",
                operator: "",
                afterValue: "",
                resultqqq: ""
            }
        },
        methods: {
            btnClick(item) {
    // 判断是否有结果，有结果后清空内容
    if (this.resultqqq) {
        this.resultqqq = "";
        this.beforeValue = "";
        this.afterValue = "";
        this.operator = "";
        // alert("请输入数字");
    }
    
    if (item == "=") {
        if (!this.beforeValue || !this.afterValue || !this.operator) {
            console.log(this.beforeValue + this.afterValue + this.operator);
            alert("请输入完整的表达式")
            return;
        }
        switch (this.operator) {
            case "+":
                this.resultqqq = this.beforeValue * 1 + this.afterValue * 1
                break;
            case "-":
                this.resultqqq = this.beforeValue * 1 - this.afterValue * 1
                break;
            case "*":
                this.resultqqq = this.beforeValue * 1 * this.afterValue * 1
                break;
            case "/":
                this.resultqqq = this.beforeValue * 1 / (this.afterValue * 1)
                break;
            default:
                break
        }
        this.resultqqq = "=" + this.resultqqq
        // alert(this.resultqqq)
        return
    }
    
    if (item == "C") {
        this.resultqqq = ""
        this.beforeValue = ""
        this.afterValue = ""
        this.operator = ""
        console.log(666);
        return
    }
    
    // 如果点击的是运算符
    if (["*", "/", "+", "-"].indexOf(item) != -1) {
        if (this.beforeValue && !this.operator) {
            this.operator = item
        } else if (this.operator) {
            // 如果已经有一个运算符，替换它
            this.operator = item
        }
        return
    }
    
    // 如果已经有运算符，输入数字应该添加到 afterValue
    if (this.operator) {
        this.afterValue += item
    } else {
        // 否则添加到 beforeValue
        this.beforeValue += item
    }
}  
        }
    }
    const vm = Vue.createApp(app).mount('#app')
</script>

</html>